<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>ref子组件调用父组件方法</title>
</head>

<body>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <div id="Application">
        <child ref="hello" @refreshdata="getData"></child>
        <button type="button" @click="getHello">拿到子组件中的方法</button>
    </div>
    <script>
        const App = Vue.createApp({
            setup() {
                const hello = Vue.ref()//获取子组件
                const getHello = () => {
                    hello.value.open();
                    //拿到子组件的方法
                }
                const getData = () => {
                    console.log("11111")
                    //拿到子组件的方法
                }
                return {
                    hello,
                    getHello,
                    getData
                }
            },
        });

        //子组件
        const childBlock = {
            setup(props, ctx) {
                const open = () => {
                    console.log("调用到了");
                    //子组件调用父组件方法
                    ctx.emit("refreshdata");
                }
                return {
                    open
                }

            },

            template: '<h1>我是子组件哦</h1>'
        }
        App.component("child", childBlock);

        //挂载
        App.mount("#Application");
    </script>
</body>

</html>